home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 2001 May / SGI IRIX Base Documentation 2001 May.iso / usr / share / catman / p_man / cat3 / perl5 / SelfLoader.z / SelfLoader
Encoding:
Text File  |  1998-10-30  |  12.1 KB  |  265 lines

  1.  
  2.  
  3.  
  4. SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr((((3333))))                                                    SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr((((3333))))
  5.  
  6.  
  7.  
  8. NNNNAAAAMMMMEEEE
  9.      SelfLoader - load functions only on demand
  10.  
  11. SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
  12.          package FOOBAR;
  13.          use SelfLoader;
  14.  
  15.          ... (initializing code)
  16.  
  17.          __DATA__
  18.          sub {....
  19.  
  20.  
  21. DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  22.      This module tells its users that functions in the FOOBAR package are to
  23.      be autoloaded from after the __DATA__ token.  See also the section on
  24.      _A_u_t_o_l_o_a_d_i_n_g in the _p_e_r_l_s_u_b manpage.
  25.  
  26.      TTTThhhheeee ________DDDDAAAATTTTAAAA________ ttttooookkkkeeeennnn
  27.  
  28.      The __DATA__ token tells the perl compiler that the perl code for
  29.      compilation is finished. Everything after the __DATA__ token is available
  30.      for reading via the filehandle FOOBAR::DATA, where FOOBAR is the name of
  31.      the current package when the __DATA__ token is reached. This works just
  32.      the same as __END__ does in package 'main', but for other modules data
  33.      after __END__ is not automatically retreivable , whereas data after
  34.      __DATA__ is.  The __DATA__ token is not recognized in versions of perl
  35.      prior to 5.001m.
  36.  
  37.      Note that it is possible to have __DATA__ tokens in the same package in
  38.      multiple files, and that the last __DATA__ token in a given package that
  39.      is encountered by the compiler is the one accessible by the filehandle.
  40.      This also applies to __END__ and main, i.e. if the 'main' program has an
  41.      __END__, but a module 'require'd (_not_ 'use'd) by that program has a
  42.      'package main;' declaration followed by an '__DATA__', then the DATA
  43.      filehandle is set to access the data after the __DATA__ in the module,
  44.      _not_ the data after the __END__ token in the 'main' program, since the
  45.      compiler encounters the 'require'd file later.
  46.  
  47.      SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr aaaauuuuttttoooollllooooaaaaddddiiiinnnngggg
  48.  
  49.      The SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr works by the user placing the __DATA__ token _a_f_t_e_r perl
  50.      code which needs to be compiled and run at 'require' time, but _b_e_f_o_r_e
  51.      subroutine declarations that can be loaded in later - usually because
  52.      they may never be called.
  53.  
  54.      The SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr will read from the FOOBAR::DATA filehandle to load in the
  55.      data after __DATA__, and load in any subroutine when it is called. The
  56.      costs are the one-time parsing of the data after __DATA__, and a load
  57.      delay for the _first_ call of any autoloaded function. The benefits
  58.      (hopefully) are a speeded up compilation phase, with no need to load
  59.      functions which are never used.
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr((((3333))))                                                    SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr((((3333))))
  71.  
  72.  
  73.  
  74.      The SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr will stop reading from __DATA__ if it encounters the
  75.      __END__ token - just as you would expect.  If the __END__ token is
  76.      present, and is followed by the token DATA, then the SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr leaves
  77.      the FOOBAR::DATA filehandle open on the line after that token.
  78.  
  79.      The SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr exports the AUTOLOAD subroutine to the package using the
  80.      SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr, and this loads the called subroutine when it is first called.
  81.  
  82.      There is no advantage to putting subroutines which will _always_ be
  83.      called after the __DATA__ token.
  84.  
  85.      AAAAuuuuttttoooollllooooaaaaddddiiiinnnngggg aaaannnndddd ppppaaaacccckkkkaaaaggggeeee lllleeeexxxxiiiiccccaaaallllssss
  86.  
  87.      A 'my $pack_lexical' statement makes the variable $pack_lexical local
  88.      _only_ to the file up to the __DATA__ token. Subroutines declared
  89.      elsewhere _cannot_ see these types of variables, just as if you declared
  90.      subroutines in the package but in another file, they cannot see these
  91.      variables.
  92.  
  93.      So specifically, autoloaded functions cannot see package lexicals (this
  94.      applies to both the SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr and the Autoloader).  The vars pragma
  95.      provides an alternative to defining package-level globals that will be
  96.      visible to autoloaded routines. See the documentation on vvvvaaaarrrrssss in the
  97.      pragma section of the _p_e_r_l_m_o_d manpage.
  98.  
  99.      SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr aaaannnndddd AAAAuuuuttttooooLLLLooooaaaaddddeeeerrrr
  100.  
  101.      The SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr can replace the AutoLoader - just change 'use AutoLoader'
  102.      to 'use SelfLoader' (though note that the SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr exports the AUTOLOAD
  103.      function - but if you have your own AUTOLOAD and are using the AutoLoader
  104.      too, you probably know what you're doing), and the __END__ token to
  105.      __DATA__. You will need perl version 5.001m or later to use this (version
  106.      5.001 with all patches up to patch m).
  107.  
  108.      There is no need to inherit from the SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr.
  109.  
  110.      The SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr works similarly to the AutoLoader, but picks up the subs
  111.      from after the __DATA__ instead of in the 'lib/auto' directory.  There is
  112.      a maintainance gain in not needing to run AutoSplit on the module at
  113.      installation, and a runtime gain in not needing to keep opening and
  114.      closing files to load subs. There is a runtime loss in needing to parse
  115.      the code after the __DATA__. Details of the AAAAuuuuttttooooLLLLooooaaaaddddeeeerrrr and another view
  116.      of these distinctions can be found in that module's documentation.
  117.  
  118.      ________DDDDAAAATTTTAAAA________,,,, ________EEEENNNNDDDD________,,,, aaaannnndddd tttthhhheeee FFFFOOOOOOOOBBBBAAAARRRR::::::::DDDDAAAATTTTAAAA ffffiiiilllleeeehhhhaaaannnnddddlllleeee....
  119.  
  120.      This section is only relevant if you want to use the FOOBAR::DATA
  121.      together with the SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr.
  122.  
  123.      Data after the __DATA__ token in a module is read using the FOOBAR::DATA
  124.      filehandle. __END__ can still be used to denote the end of the __DATA__
  125.      section if followed by the token DATA - this is supported by the
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr((((3333))))                                                    SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr((((3333))))
  137.  
  138.  
  139.  
  140.      SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr. The FOOBAR::DATA filehandle is left open if an __END__
  141.      followed by a DATA is found, with the filehandle positioned at the start
  142.      of the line after the __END__ token. If no __END__ token is present, or
  143.      an __END__ token with no DATA token on the same line, then the filehandle
  144.      is closed.
  145.  
  146.      The SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr reads from wherever the current position of the
  147.      FOOBAR::DATA filehandle is, until the EOF or __END__. This means that if
  148.      you want to use that filehandle (and ONLY if you want to), you should
  149.      either
  150.  
  151.      1. Put all your subroutine declarations immediately after the __DATA__
  152.      token and put your own data after those declarations, using the __END__
  153.      token to mark the end of subroutine declarations. You must also ensure
  154.      that the SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr reads first by  calling 'SelfLoader->_l_o_a_d__s_t_u_b_s();',
  155.      or by using a function which is selfloaded;
  156.  
  157.      or
  158.  
  159.      2. You should read the FOOBAR::DATA filehandle first, leaving the handle
  160.      open and positioned at the first line of subroutine declarations.
  161.  
  162.      You could conceivably do both.
  163.  
  164.      CCCCllllaaaasssssssseeeessss aaaannnndddd iiiinnnnhhhheeeerrrriiiitttteeeedddd mmmmeeeetttthhhhooooddddssss....
  165.  
  166.      For modules which are not classes, this section is not relevant.  This
  167.      section is only relevant if you have methods which could be inherited.
  168.  
  169.      A subroutine stub (or forward declaration) looks like
  170.  
  171.        sub stub;
  172.  
  173.      i.e. it is a subroutine declaration without the body of the subroutine.
  174.      For modules which are not classes, there is no real need for stubs as far
  175.      as autoloading is concerned.
  176.  
  177.      For modules which ARE classes, and need to handle inherited methods,
  178.      stubs are needed to ensure that the method inheritance mechanism works
  179.      properly. You can load the stubs into the module at 'require' time, by
  180.      adding the statement 'SelfLoader->_l_o_a_d__s_t_u_b_s();' to the module to do
  181.      this.
  182.  
  183.      The alternative is to put the stubs in before the __DATA__ token BEFORE
  184.      releasing the module, and for this purpose the Devel::SelfStubber module
  185.      is available.  However this does require the extra step of ensuring that
  186.      the stubs are in the module. If this is done I strongly recommend that
  187.      this is done BEFORE releasing the module - it should NOT be done at
  188.      install time in general.
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr((((3333))))                                                    SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr((((3333))))
  203.  
  204.  
  205.  
  206. MMMMuuuullllttttiiiipppplllleeee ppppaaaacccckkkkaaaaggggeeeessss aaaannnndddd ffffuuuullllllllyyyy qqqquuuuaaaalllliiiiffffiiiieeeedddd ssssuuuubbbbrrrroooouuuuttttiiiinnnneeee nnnnaaaammmmeeeessss
  207.      Subroutines in multiple packages within the same file are supported - but
  208.      you should note that this requires exporting the SelfLoader::AUTOLOAD to
  209.      every package which requires it. This is done automatically by the
  210.      SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr when it first loads the subs into the cache, but you should
  211.      really specify it in the initialization before the __DATA__ by putting a
  212.      'use SelfLoader' statement in each package.
  213.  
  214.      Fully qualified subroutine names are also supported. For example,
  215.  
  216.         __DATA__
  217.         sub foo::bar {23}
  218.         package baz;
  219.         sub dob {32}
  220.  
  221.      will all be loaded correctly by the SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr, and the SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr will
  222.      ensure that the packages 'foo' and 'baz' correctly have the SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr
  223.      AUTOLOAD method when the data after __DATA__ is first parsed.
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.                                                                         PPPPaaaaggggeeee 4444
  262.  
  263.  
  264.  
  265.